#include<iostream>
#include<string>
#include<set>
#include<cstring>
#include<vector>
using namespace std;
const int N = 2e5 + 10, M = N;
int n, d;
int h[N], e[M], ne[M], idx;
bool st[N];
vector<int>ans;
int flag;
int idx1, idx2;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

bool dfs(int u)
{
	st[u] = 1;
	ans.push_back(u);
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (st[j]) {
			idx1 = j;
			idx2 = u;
			return 1;
		}
		else {
			if (dfs(j))return 1;
		}
	}
	return 0;
}
int main()
{
	cin >> n;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		add(i, x);
	}
	for (int i = 1; i <= n; i++) {
		if (!st[i]) {
			ans.clear();
			if (dfs(i)) {
				break;
			}
		}
	}
	int idx = 0;
	/*cout << "idx1==" << idx1 << endl;
	cout << "idx2==" << idx2 << endl;*/
	for (int i = 0; i < ans.size(); i++) {
		if (ans[i] == idx1) {
			idx = i;
			break;
		}
	}
	cout << ans.size() - idx << endl;
	for (int i = idx; i < ans.size(); i++) {
		cout << ans[i] << " ";
	}
	return 0;
}